package controllers

import (
	"HeartTimesCrm/models"
	"github.com/astaxie/beego"
)

//用户注册登录相关API
type UserEntryController struct {
	beego.Controller
}

func (this *UserEntryController) URLMapping() {
	this.Mapping("Check", this.Check)
	this.Mapping("Register", this.Register)
	this.Mapping("QuickLogin", this.QuickLogin)
	this.Mapping("Login", this.Login)
	this.Mapping("Logout", this.Logout)
}

// @router /check [get]
func (this *UserEntryController) Check() {
	phone := this.GetString("phone")
	beego.BeeLogger.Debug("UserEntry::Check Proccess; phone: %s", phone)

	var errCause string
	if phone == "" {
		errCause = "phone is empty!"
	} else if models.CheckUser(phone) {
		errCause = "phone has been registered!"
	}

	res := new(Response)
	if errCause != "" {
		res.ErrNo = -1
		res.Data = errCause
		beego.BeeLogger.Debug("UserEntry::Check Proccess; Err: %s", errCause)
	}

	this.Data["json"] = res
	this.ServeJson()
}

// @router /register [post]
func (this *UserEntryController) Register() {
	phone := this.GetString("phone")
	passwd := this.GetString("passwd")
	beego.BeeLogger.Debug("UserEntry::Register Proccess; phone: %s", phone)

	var errCause string
	res := new(Response)
	if phone == "" {
		errCause = "phone is empty!"
	} else if passwd == "" {
		errCause = "passwd is empty!"
	} else {
		user, err := models.CreateUser(phone, passwd)
		if err != nil {
			errCause = err.Error()
		} else {
			utag := &UserTag{user.Id, *user.ImageId}
			this.SetSession("utag", utag)
			res.Data = utag
		}
	}
	if errCause != "" {
		res.ErrNo = -1
		res.Data = errCause
		beego.BeeLogger.Debug("UserEntry::Check Proccess; Err: %s", errCause)
	}

	this.Data["json"] = res
	this.ServeJson()
}

// @router /quicklogin [get]
func (this *UserEntryController) QuickLogin() {
	phone := this.GetString("phone")
	beego.BeeLogger.Debug("UserEntry::QuickLogin Proccess; phone: %s", phone)

	var errCause string
	res := new(Response)
	if phone == "" {
		errCause = "phone is empty!"
	} else {
		user, err := models.QuickLogin(phone)
		if err != nil {
			errCause = err.Error()
		} else {
			utag := &UserTag{user.Id, *user.ImageId}
			this.SetSession("utag", utag)
			res.Data = utag
		}
	}

	if errCause != "" {
		res.ErrNo = -1
		res.Data = errCause
		beego.BeeLogger.Debug("UserEntry::QuickLogin Proccess; Err: %s", errCause)
	}

	this.Data["json"] = res
	this.ServeJson()
}

// @router /login [get]
func (this *UserEntryController) Login() {
	phone := this.GetString("phone")
	passwd := this.GetString("passwd")
	beego.BeeLogger.Debug("UserEntry::Login; phone: %s", phone)

	var errCause string
	res := new(Response)
	if phone == "" {
		errCause = "phone is empty!"
	} else if passwd == "" {
		errCause = "passwd is empty!"
	} else {
		user, err := models.Login(phone, passwd)
		if err != nil {
			errCause = err.Error()
		} else {
			utag := &UserTag{user.Id, *user.ImageId}
			this.SetSession("utag", utag)
			res.Data = utag
		}
	}
	if errCause != "" {
		res.ErrNo = -1
		res.Data = errCause
		beego.BeeLogger.Debug("UserEntry::Login Proccess; Err: %s", errCause)
	}

	this.Data["json"] = res
	this.ServeJson()
}

// @router /logout [get]
func (this *UserEntryController) Logout() {
	data := "Logout success"
	logOutMess := "UserEntry::Logout;"
	if beego.RunMode == "dev" {
		data = "Logout success; user not login!"
		value := this.GetSession("utag")
		if utag, ok := value.(*UserTag); ok {
			if user, err := models.GetBaseInfo(utag.Uid); err == nil {
				data = "Logout success; user: " + *user.Phone
			}
		}
		logOutMess = "UserEntry::Logout; " + data
	}
	this.DelSession("utag")
	beego.BeeLogger.Debug(logOutMess)

	this.Data["json"] = &Response{0, data}
	this.ServeJson()
}
